//
//  PublicDefine.swift
//  Unzip
//
//  Created by zhibao on 2024/2/2.
//

import Foundation
func DLog<T>(message: T,
                 file: String = #file,
                 line: Int = #line)
{
    let fileName = (file as NSString).lastPathComponent
    print("\(fileName)第\(line)行：\(message)")
}

let SCREEN_WIDTH = UIScreen.main.bounds.width
let SCREEN_HEIGHT = UIScreen.main.bounds.height
let kMainScreenBounds = UIScreen.main.bounds

//颜色切换定义

let MYRed = MYColor.color(withHexString:"#E04148")
let MYOrange = MYColor.color(withHexString:"#FFD640")
let MYBlue = MYColor.color(withHexString:"#2666e4")
let MYGreen = MYColor.color(withHexString:"#1EA61E")
let ThemeColor = MYColor.color(withHexString:"#0F1011")
let ciThemeColor = MYColor.color(withHexString:"#25282D")
let MYBlock = MYColor.color(withHexString:"#333333")

func LocalizedString(_ key:String) -> String {
    return NSLocalizedString(key, comment: "")
}

//自定义字体或系统其他字体
func kNumberFONT(_ R:CGFloat) -> UIFont!{
    return UIFont.init(name: "DIN Alternate", size: pixw(p: R)) ?? MY_SCALE_FONT(pixw(p: R))
}

//普通字体
func MY_SCALE_FONT(_ R:CGFloat) -> UIFont! {
    return UIFont.systemFont(ofSize: pixw(p: R))
}

//默认加粗字体
func MY_SCALE_BoldFont(_ R:CGFloat) -> UIFont! {
    return UIFont.boldSystemFont(ofSize: pixw(p: R))
}

//PingFangSC-Regular
func MY_SCALE_RegularFONT(_ R:CGFloat) -> UIFont! {
    return UIFont(name: "PingFangSC-Light", size: pixw(p: R)) ?? MY_SCALE_FONT(pixw(p: R))
}

func MY_SCALE_HeavyFONT(_ R:CGFloat) -> UIFont! {
    return UIFont(name: "PingFangSC-Semibold", size: pixw(p: R)) ?? MY_SCALE_FONT(pixw(p: R))
}

func MY_SCALE_LightFONT(_ R:CGFloat) -> UIFont! {
    return UIFont(name: "PingFangSC-Light", size: pixw(p: R)) ?? MY_SCALE_FONT(pixw(p: R))
}

func MY_SCALE_UltralightFONT(_ R:CGFloat) -> UIFont! {
    return UIFont(name: "PingFangSC-Ultralight", size: pixw(p: R)) ?? MY_SCALE_FONT(pixw(p: R))
}

func MY_SCALE_ThinFONT(_ R:CGFloat) -> UIFont! {
    return UIFont(name: "PingFangSC-Thin", size: pixw(p: R)) ?? MY_SCALE_FONT(R)
}

func MY_SCALE_MediumFONT(_ R:CGFloat) -> UIFont! {
    return UIFont(name: "PingFangSC-Medium", size: pixw(p: R)) ?? MY_SCALE_FONT(pixw(p: R))
}

func MY_SCALE_SemiboldFONT(_ R:CGFloat) -> UIFont! {
    return UIFont(name: "PingFangSC-Semibold", size: pixw(p: R)) ?? MY_SCALE_FONT(pixw(p: R))
}

//heavy
func KHFONT(_ R:CGFloat) -> UIFont {
    return UIFont.systemFont(ofSize: pixw(p: R), weight: .heavy)
}


//根据当前屏幕宽度得到宽度
func pixw(p:CGFloat) ->CGFloat{
    return (SCREEN_WIDTH/395.0)*p
}

//根据当前屏幕高度得到高度
func pixh(p:CGFloat) ->CGFloat{
    return (SCREEN_HEIGHT/395)*p
}

func RGBA(_ r:CGFloat,_ g:CGFloat,_ b:CGFloat,_ a:CGFloat) ->UIColor{
    return UIColor(red: r / 255, green: g / 255, blue: b / 255, alpha: a)
}

/// 顶部安全区高度
func safeDistanceTop() -> CGFloat {
    let scene = UIApplication.shared.connectedScenes.first
    guard let windowScene = scene as? UIWindowScene else { return 0 }
    guard let window = windowScene.windows.first else { return 0 }
    return window.safeAreaInsets.top
}

/// 底部安全区高度
func safeDistanceBottom() -> CGFloat {
    let scene = UIApplication.shared.connectedScenes.first
    guard let windowScene = scene as? UIWindowScene else { return 0 }
    guard let window = windowScene.windows.first else { return 0 }
    return window.safeAreaInsets.bottom
}

/// 顶部状态栏高度（包括安全区）
 func statusBarHeight() -> CGFloat {
    var statusBarHeight: CGFloat = 0
     let scene = UIApplication.shared.connectedScenes.first
     guard let windowScene = scene as? UIWindowScene else { return 0 }
     guard let statusBarManager = windowScene.statusBarManager else { return 0 }
     statusBarHeight = statusBarManager.statusBarFrame.height
    return statusBarHeight
}

/// 导航栏高度
func navigationBarHeight() -> CGFloat {
    return 44.0
}

/// 状态栏+导航栏的高度
func navigationFullHeight() -> CGFloat {
    return statusBarHeight() + navigationBarHeight()
}

/// 底部导航栏高度
 func tabBarHeight() -> CGFloat {
    return 49.0
}

/// 底部导航栏高度（包括安全区）
 func tabBarFullHeight() -> CGFloat {
    return tabBarHeight() + safeDistanceBottom()
}

/// 获取当前window
func keyWindow() -> UIWindow {
       if #available(iOS 15.0, *) {
           let keyWindow = UIApplication.shared.connectedScenes
               .map({ $0 as? UIWindowScene })
               .compactMap({ $0 })
               .first?.windows.first ?? UIWindow()
           return keyWindow
       }else {
           let keyWindow = UIApplication.shared.windows.first ?? UIWindow()
           return keyWindow
       }
   }
